package com.miao.util.ip;

import java.io.*;

/**
 * Created by Shaokang on 2015/5/22.
 */
public class GetMac {

    /**
     * java获取客户端网卡的MAC地址
     *
     * @param args
     */
    public static void main(String[] args) {
        String mac = getMacAddressIP("127.0.0.1");
        System.out.println(mac);
    }

    // 1.获取客户端ip地址( 这个必须从客户端传到后台)：
    // jsp页面下，很简单，request.getRemoteAddr() ;
    // 因为系统的VIew层是用JSF来实现的，因此页面上没法直接获得类似request，在bean里做了个强制转换

    // public String getMyIP() {
    // try {
    // FacesContext fc = FacesContext.getCurrentInstance();
    // HttpServletRequest request = (HttpServletRequest) fc
    // .getExternalContext().getRequest();
    // return request.getRemoteAddr();
    // } catch (Exception e) {
    // e.printStackTrace();
    // }
    // return "";
    // }

    // 2.获取客户端mac地址
    // 调用window的命令，在后台Bean里实现 通过ip来获取mac地址。方法如下：

    public static String getMacAddressIP(String remotePcIP) {
        String str = "";
        String macAddress = "";
        try {
            Process pp = Runtime.getRuntime().exec("nbtstat -A " + remotePcIP);
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            for (int i = 1; i < 100; i++) {
                str = input.readLine();
                if (str != null) {
                    if (str.indexOf("MAC Address") > 1) {
                        macAddress = str.substring(
                                str.indexOf("MAC Address") + 14, str.length());
                        break;
                    }
                }
            }
        } catch (IOException ex) {
        }
        return macAddress;
    }

    // 运行速度【快】
    public String getMAC() {
        String mac = null;
        try {
            Process pro = Runtime.getRuntime().exec("cmd.exe /c ipconfig/all");

            InputStream is = pro.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String message = br.readLine();

            int index = -1;
            while (message != null) {
                if ((index = message.indexOf("Physical Address")) > 0) {
                    mac = message.substring(index + 36).trim();
                    break;
                }
                message = br.readLine();
            }
            System.out.println(mac);
            br.close();
            pro.destroy();
        } catch (IOException e) {
            System.out.println("Can't get mac address!");
            return null;
        }
        return mac;
    }

    // 运行速度【慢】
    public String getMAC(String ip) {
        String str = null;
        String macAddress = null;
        try {
            Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
            InputStreamReader ir = new InputStreamReader(p.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            for (; true; ) {
                str = input.readLine();
                if (str != null) {
                    if (str.indexOf("MAC Address") > 1) {
                        macAddress = str
                                .substring(str.indexOf("MAC Address") + 14);
                        break;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace(System.out);
            return null;
        }
        return macAddress;
    }
}
